package com.example.dmallOrder.common.dmall.util;

import cn.hutool.core.util.StrUtil;
import com.dmall.sdk.DmallTokenClient;
import com.dmall.sdk.response.DmallResponse;
import com.dmall.sdk.response.token.TokenResponse;
import com.example.dmallOrder.config.DmallConfig.DmallConstant;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DmallTokenUntil {
    /*public static String getToken() {
        String dtStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("appKey", DmallConstant.APP_KEY.getValue());
        map.put("grant_type", "client_credentials");
        map.put("code", DmallConstant.CODE.getValue());
        map.put("sign", SignUtil2.generateTokenSignature( DmallConstant.APP_KEY.getValue(),DmallConstant.CODE.getValue() , dtStr, "HmacSHA256", DmallConstant.APP_SECRET.getValue(), new HashMap<>()));
        map.put("timestamp", dtStr);
        String respStr = HttpUtil.httpPostRequest(DmallConstant.TOKEN_SERVER_URL.getValue(), map);
        return respStr;
    }*/
    /*public static String refreshToken() {
        String oldToken = getToken();
        DmallTokenClient dmallTokenClient = new DmallTokenClient(DmallConstant.REFRESH_TOKEN_NEW_URI.getValue(), DmallConstant.APP_KEY.getValue(), DmallConstant.APP_SECRET.getValue(), DmallConstant.CODE.getValue());
        DmallResponse<TokenResponse> dmallResponse = dmallTokenClient.refreshToken(oldToken, DmallConstant.CODE.getValue());
    }*/

    public static String getTokenNew() {
        try {
            String tokenServerUrl = DmallConstant.TOKEN_SERVER_URL.getValue();
            // 获取token 有效期一年
            DmallTokenClient dmallTokenClient = new DmallTokenClient(tokenServerUrl, DmallConstant.APP_KEY.getValue(), DmallConstant.APP_SECRET.getValue(), DmallConstant.CODE.getValue());
            DmallResponse<TokenResponse> tokenResponse = dmallTokenClient.generateTokenNew();
            return tokenResponse.getData().getToken();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
//        throw new BusinessException("未获取到");
        return StrUtil.EMPTY;
    }

}
